//
//  ContentButton.swift
//  SalonLab
//
//  Created by hongyan.yang on 2021/9/27.
//  Copyright © 2021 iconmobile gmbh. All rights reserved.
//
//  Usage: 
//  ```
//          button.style = .light
//          botton.setTitleColor(.gray, for: .normal)
//          button.layer.shadowOpacity = 0.3
//  ```


import UIKit

protocol Contentable where Self: UIView {
    var content: Content { get set }
}

enum Content {
    case `default`
    case inProgress
}

class ContentButton: UIButton, Contentable {
    // MARK: Enums
    enum Style {
        case light, dark, underlined,icon
    }
    // MARK: Contentable
    var content: Content = .default {
        didSet {
            guard content != oldValue else { return }
            switch content {
            case .inProgress:
                isEnabled = false
            case .default:
                isEnabled = true
            }
            contentInterfaceUpdate()
        }
    }

    // MARK: Properties
    private weak var _underlineView: UIView?
    private var disableTitleColor: UIColor?
    public var style: Style? {
        didSet{
            switch style {
            case .icon:
                backgroundView?.backgroundColor = UIColor.clear
                showUnderline = false
                // customize indicator
                activityIndicator?.style = .gray
                // customize insets
                let insets = UIEdgeInsets.zero
                titleEdgeInsets = insets
                contentEdgeInsets = insets
                // customize shadow
                layer.shadowColor = UIColor.black.cgColor
                layer.shadowOffset = CGSize.init(width: 5, height: 10)
                layer.shadowRadius = 30
                layer.shadowOpacity = 0.6
            case .dark:
                backgroundView?.backgroundColor = UIColor.black
                showUnderline = false
                setTitleColor(.white, for: .normal)
                // customize indicator
                activityIndicator?.style = .white
                // customize insets
                let insets = UIEdgeInsets(top: 0, left: 35, bottom: 0, right: 35)
                titleEdgeInsets = insets
                contentEdgeInsets = insets
                // customize corner
                layer.cornerRadius = 20
                backgroundView.layer.cornerRadius = 20
                // customize shadow
                layer.shadowColor = UIColor.black.cgColor
                layer.shadowOffset = CGSize.init(width: 5, height: 10)
                layer.shadowRadius = 30
                layer.shadowOpacity = 0.6
            case .light:
                backgroundView?.backgroundColor = UIColor.white
                showUnderline = false
                setTitleColor(.black, for: .normal)
                setTitleColor(.white, for: .disabled)
                // customize indicator
                activityIndicator?.style = .gray
                // customize insets
                let insets = UIEdgeInsets(top: 0, left: 35, bottom: 0, right: 35)
                titleEdgeInsets = insets
                contentEdgeInsets = insets
                // customize corner
                layer.cornerRadius = 20
                backgroundView.layer.cornerRadius = 20
                // customize shadow
                layer.shadowColor = UIColor.black.cgColor
                layer.shadowOffset = CGSize.init(width: 5, height: 10)
                layer.shadowRadius = 30
                layer.shadowOpacity = 0.6
            case .underlined:
                backgroundView.isHidden = true
                showUnderline = true
                _underlineView?.backgroundColor = UIColor(hexString: "717171")
                setTitleColor(_underlineView?.backgroundColor, for: .normal)
                setTitleColor(.lightGray, for: .disabled)
                titleLabel?.font = UIFont.systemFont(ofSize: 16)
                // customize indicator
                activityIndicator?.style = .gray
            case .none:
                break
            }
        }
    }
    public var showUnderline = false {
        didSet {
            _underlineView?.isHidden = !showUnderline
        }
    }
    public weak var backgroundView: UIView!
    public weak var activityIndicator: UIActivityIndicatorView?

    
    // MARK: - Init
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        commonInit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commonInit()
    }
    func commonInit() {
        backgroundColor = .clear

        // Background view
        let backgroundView = UIView()
        backgroundView.isUserInteractionEnabled = false
        backgroundView.layer.masksToBounds = false
        addSubview(backgroundView)
        backgroundView.translatesAutoresizingMaskIntoConstraints = false
        let hConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|[backgroundView]|", metrics: nil, views: ["backgroundView": backgroundView])
        let vConstraints = NSLayoutConstraint.constraints(withVisualFormat: "V:|[backgroundView]|", metrics: nil, views: ["backgroundView": backgroundView])
        NSLayoutConstraint.activate(hConstraints + vConstraints)
        self.backgroundView = backgroundView

        // Title label
        titleLabel?.numberOfLines = 1;
        titleLabel?.adjustsFontSizeToFitWidth = true
        titleLabel?.lineBreakMode = .byClipping

        titleEdgeInsets = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
        contentEdgeInsets = titleEdgeInsets
        // Underline view
        let underlineView = UIView()
        underlineView.backgroundColor = .black
        addSubview(underlineView)
        underlineView.translatesAutoresizingMaskIntoConstraints = false
        underlineView.heightAnchor.constraint(equalToConstant: 1).isActive = true
        underlineView.centerXAnchor.constraint(equalTo: titleLabel?.centerXAnchor ?? centerXAnchor).isActive = true
        underlineView.widthAnchor.constraint(equalTo: titleLabel?.widthAnchor ?? widthAnchor, constant: 6).isActive = true
        if let titleLabel = titleLabel {
            underlineView.bottomAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 1).isActive = true
        }
        underlineView.isHidden = !showUnderline
        self._underlineView = underlineView
        
        // Indicator
        let activityIndicator = UIActivityIndicatorView()
        activityIndicator.isHidden = true
        addSubview(activityIndicator)
        activityIndicator.translatesAutoresizingMaskIntoConstraints = false
        activityIndicator.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        activityIndicator.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        self.activityIndicator = activityIndicator
        
        contentInterfaceUpdate()
    }
}

extension ContentButton {
    override var isEnabled: Bool {
        didSet{
            switch style {
            case .dark:
                if isEnabled {
                    backgroundView?.backgroundColor = .black
                }else{
                    backgroundView?.backgroundColor = content == .inProgress ? .black : UIColor(hexString:"c8c8c8")
                }
                layer.shadowOpacity = isEnabled ? 0.6 : 0
            case .light:
                backgroundView?.backgroundColor = isEnabled ? UIColor.white : UIColor(hexString: "c8c8c8")
                layer.shadowOpacity = isEnabled ? 0.6 : 0
            case .underlined:
                _underlineView?.backgroundColor = isEnabled ? UIColor(hexString: "717171") : .lightGray
            case .icon, .none:
                break
            }
        }
    }
    func contentInterfaceUpdate() {
        switch content {
        case .default:
            activityIndicator?.stopAnimating()
            activityIndicator?.isHidden = true
            _underlineView?.isHidden = !showUnderline
            setTitleColor(disableTitleColor, for: .disabled)
        case .inProgress:
            activityIndicator?.isHidden = false
            activityIndicator?.startAnimating()
            _underlineView?.isHidden = true
            disableTitleColor = titleColor(for: .disabled)
            setTitleColor(.clear, for: .disabled)
        }
    }
}
